package com.itcpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.serializer.CalendarCodec;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.entity.Result;
import com.itcpeng.service.MemberService;
import com.itcpeng.service.ReportService;
import com.itcpeng.util.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.Month;
import java.util.*;

@RestController
@RequestMapping(value = "/report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private ReportService reportService;

    @GetMapping(value = "/getMemberReport")
    public Result getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        //计算过去12个月,将每个月放入list
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);//按照月进行计算

        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            try {
                String date = DateUtils.parseDate2String(time, "yyyy.MM");
                months.add(date);
                calendar.add(Calendar.MONTH, 1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        map.put("months", months);
        List<Integer> memberCount = memberService.findMemberCountByMonth(months);
        map.put("memberCount", memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    @PostMapping(value = "/getMemberReport02")
    public Result getMemberReport02(String fei,String fei02) {
        fei = "2019-3-01";
        fei02 = "2019-11-31";
        try {
            Map<String, Object> map = reportService.getRangeMemberReport(fei,fei02);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    @GetMapping(value = "/getPackageReport")
    public Result getPackageReport() {
        //查询套餐占比数据   每个套餐就是一个map
        List<Map<String, Object>> count = reportService.getPackageReport();
        //组装套餐列表数据
        List<String> names = new ArrayList<String>();
        if (count != null) {
            for (Map<String, Object> map : count) {
                //map  一个套餐
                names.add((String) map.get("name"));
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("packageNames", names);
        map.put("packageCount", count);
        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
    }

    //POL报表
    @PostMapping(value = "/getBusinessReportData")
    public Result getBusinessReportData() {
        Map<String, Object> map = reportService.getBusinessReportData();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
    }


    //会员按照年龄统计 饼状图
    @RequestMapping("/getMemberReportByAge")
    public Result getUserReport() {
        List<Map<String, Object>> list = memberService.getMemberReportByAge();


        Map<String,Object> map = new HashMap<>();
        map.put("ageCount",list);

        ArrayList<String> ageAreas = new ArrayList<>();
        for (Map<String, Object> m : list) {
            String name = (String) m.get("name");
            ageAreas.add(name);
        }
        map.put("ageAreas",ageAreas);

        return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }



    @GetMapping("/getMemberReportBySex")
    public Result findCount() {
        List<Map<String, Object>> list = memberService.getMemberReportBySex();
        List<String> sex=new ArrayList<>();
        for (Map<String, Object> map : list) {
            sex.add((String)map.get("name"));
        }
        Map<String,Object>findCount=new HashMap<>();
        findCount.put("gender",sex);
        findCount.put("statistical",list);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,findCount);
    }
}
